/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#pragma once

#include <cstdint>
#include <string>
#include <sstream>

namespace Hdfs {
namespace Internal {

/**
 * This class extends the primary identifier of a Datanode with ephemeral
 * state, eg usage information, current administrative state, and the
 * network location that is communicated to clients.
 */
class DatanodeInfo {
public:
    DatanodeInfo() : xferPort(0), infoPort(0), ipcPort(0),
        ipAddr("null"), hostName("null"), datanodeId("null"), location("null") {}

    const std::string & getHostName() const {
        return hostName;
    }

    void setHostName(const std::string & hostName) {
        this->hostName = hostName;
    }

    uint32_t getInfoPort() const {
        return infoPort;
    }

    void setInfoPort(uint32_t infoPort) {
        this->infoPort = infoPort;
    }

    const std::string & getIpAddr() const {
        return ipAddr;
    }

    void setIpAddr(const std::string & ipAddr) {
        this->ipAddr = ipAddr;
    }

    uint32_t getIpcPort() const {
        return ipcPort;
    }

    void setIpcPort(uint32_t ipcPort) {
        this->ipcPort = ipcPort;
    }

    const std::string & getDatanodeId() const {
        return datanodeId;
    }

    void setDatanodeId(const std::string & storageId) {
        this->datanodeId = storageId;
    }

    uint32_t getXferPort() const {
        return xferPort;
    }

    void setXferPort(uint32_t xferPort) {
        this->xferPort = xferPort;
    }

    const std::string formatAddress() const {
        std::stringstream ss;
        ss.imbue(std::locale::classic());
        ss << hostName << "(" << getIpAddr() << ")";
        return ss.str();
    }

    bool operator <(const DatanodeInfo & other) const {
        return datanodeId < other.datanodeId;
    }

    bool operator ==(const DatanodeInfo & other) const {
        return this->datanodeId == other.datanodeId
               && this->ipAddr == other.ipAddr;
    }

    const std::string & getLocation() const {
        return location;
    }

    void setLocation(const std::string & location) {
        this->location = location;
    }

    std::string getXferAddr() const {
        std::stringstream ss;
        ss.imbue(std::locale::classic());
        ss << getIpAddr() << ":" << getXferPort();
        return ss.str();
    }

private:
    uint32_t xferPort;
    uint32_t infoPort;
    uint32_t ipcPort;
    std::string ipAddr;
    std::string hostName;
    std::string datanodeId;
    std::string location;
};

}
}
